/**
 *EffectPane.java
 *@anthor Daniel Liu
 *2006-5-9 ����04:59:51
 **/
package org.lex.swing.fontchooser.advanced;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class AdvancedPane extends JPanel {
	private ActionHandler actionHandler = null;
	private JLabel promptLabel = null;
	private JList attributesList = null;
	private JScrollPane attributesScrollPane = null;
	private JPanel controlPane = null;
	private JCheckBox checkBox = null;

	public AdvancedPane() {
		super();
		initialize();
	}

	private void initialize() {
		promptLabel = new JLabel();
		promptLabel.setText("Available attributes:");
		this.setLayout(new BorderLayout());
		this.setSize(300, 200);
		this.add(promptLabel, BorderLayout.NORTH);
		this.add(getAttributesScrollPane(), BorderLayout.CENTER);
		this.add(getControlPane(), BorderLayout.SOUTH);
	}

	private JList getAttributesList() {
		if (attributesList == null) {
			AdvancedFontAttribute[] all = AdvancedFontAttribute.values();
			String[] names = new String[all.length];
			for (int i = 0; i < all.length; i++)
				names[i] = all[i].getDiaplayName();
			attributesList = new JList(names);
			attributesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			attributesList.addListSelectionListener(new ListSelectionHandler());
		}
		return attributesList;
	}

	private JScrollPane getAttributesScrollPane() {
		if (attributesScrollPane == null) {
			attributesScrollPane = new JScrollPane();
			attributesScrollPane.setViewportView(getAttributesList());
		}
		return attributesScrollPane;
	}

	private JPanel getControlPane() {
		if (controlPane == null) {
			controlPane = new JPanel();
			controlPane.setLayout(new FlowLayout());
			controlPane.add(getCheckBox(), null);
		}
		return controlPane;
	}

	private JCheckBox getCheckBox() {
		if (checkBox == null) {
			checkBox = new JCheckBox();
			checkBox.setText("Enable");
			checkBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
		}
		return checkBox;
	}

	ActionHandler getActionHandler() {
		if (null == this.actionHandler)
			this.actionHandler = new ActionHandler();
		return actionHandler;
	}

	class ActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
		}
	}

	class ListSelectionHandler implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (true == e.getValueIsAdjusting())
				return;

			Object newValue = getAttributesList().getSelectedValue();
			if (null == newValue)
				return;
			AdvancedFontAttribute afa = null;
			afa = AdvancedFontAttribute.parseFromDiaplayName(newValue.toString());
			System.out.println(afa.getDiaplayName());
			if (afa.getValueClass() == Boolean.class) {
				getCheckBox().setEnabled(true);
			} else {
				getCheckBox().setEnabled(false);
			}
		}
	}

}
